package com.lemontree.mbg.support;

import com.lemontree.mbg.MbgHelper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @program: custom-mbg
 * @description: 解析参数
 * @author: Lemontree
 * @create: 2018-05-08 21:33
 **/
public class PropertiesUtil {
    // 配置文件
    public static final String CONFIG_PROPERTIES = "/common_config.properties";

    public static final String KEY_JAVA_MAPPER_PACKAGE = "java.mapper.targetModelPackage";
    public static final String VAL_JAVA_MAPPER_PACKAGE;

    public static final String KEY_JAVA_MAPPER_PROJECT = "java.mapper.targetProject";
    public static final String VAL_JAVA_MAPPER_PROJECT;
    // 父类
    private static final String KEY_SUPER_CLASS = "class.superclass";
    private static final String KEY_SUPER_CLASS_PACKAGE = "class.superclass.package";

    public static final String VAL_SUPER_CLASS;
    public static final String VAL_SUPER_CLASS_PACKAGE;

    // 注解
    private static final String KEY_FIELD_ANNOTATION = "field.annotation";
    private static final String KEY_FIELD_ANNOTATION_TABLE_FIELD = "field.annotation.table.field";
    private static final String KEY_FIELD_ANNOTATION_PACKAGE = "field.annotation.package";

    private static final String KEY_CLASS_ANNOTATION = "class.annotation";
    private static final String KEY_CLASS_ANNOTATION_PACKAGE = "class.annotation.package";

    public static final String VAL_FIELD_ANNOTATION;
    public static final String VAL_FIELD_ANNOTATION_TABLE_FIELD;
    public static final String VAL_FIELD_ANNOTATION_PACKAGE;

    public static final String VAL_CLASS_ANNOTATION;
    public static final String VAL_CLASS_ANNOTATION_PACKAGE;

    // split后的列表
    public static final List<String> FIELD_ANNOTATIONS;
    public static final List<String> FIELD_ANNOTATION_TABLE_FIELD;
    public static final List<String> FIELD_ANNOTATION_PACKAGES;

    public static final List<String> CLASS_ANNOTATIONS;
    public static final List<String> CLASS_ANNOTATION_PACKAGES;

    static {
        Properties properties = new Properties();
        try {
            properties.load(MbgHelper.class
                    .getResourceAsStream(CONFIG_PROPERTIES));
        } catch(IOException e) {
            e.printStackTrace();
        }
        // 读取配置文件
        // 父类
        VAL_SUPER_CLASS = properties.getProperty(KEY_SUPER_CLASS);
        VAL_SUPER_CLASS_PACKAGE = properties
                .getProperty(KEY_SUPER_CLASS_PACKAGE);
        // 注解
        VAL_FIELD_ANNOTATION = properties
                .getProperty(KEY_FIELD_ANNOTATION);
        VAL_FIELD_ANNOTATION_TABLE_FIELD = properties
                .getProperty(KEY_FIELD_ANNOTATION_TABLE_FIELD);
        VAL_FIELD_ANNOTATION_PACKAGE = properties
                .getProperty(KEY_FIELD_ANNOTATION_PACKAGE);

        VAL_CLASS_ANNOTATION = properties
                .getProperty(KEY_CLASS_ANNOTATION);
        VAL_CLASS_ANNOTATION_PACKAGE = properties
                .getProperty(KEY_CLASS_ANNOTATION_PACKAGE);

        // 字段注解按 | 分隔
        FIELD_ANNOTATIONS = StringUtils.isBlank(VAL_FIELD_ANNOTATION)
                ? null
                : Arrays.asList(VAL_FIELD_ANNOTATION.split("\\|"));
        FIELD_ANNOTATION_PACKAGES = StringUtils
                .isBlank(VAL_FIELD_ANNOTATION_PACKAGE) ? null
                        : Arrays.asList(
                                VAL_FIELD_ANNOTATION_PACKAGE.split("\\|"));
        FIELD_ANNOTATION_TABLE_FIELD = StringUtils
                .isBlank(VAL_FIELD_ANNOTATION_TABLE_FIELD) ? null
                        : Arrays.asList(VAL_FIELD_ANNOTATION_TABLE_FIELD
                                .split("\\|"));

        CLASS_ANNOTATIONS = StringUtils.isBlank(VAL_CLASS_ANNOTATION)
                ? null
                : Arrays.asList(VAL_CLASS_ANNOTATION.split("\\|"));
        CLASS_ANNOTATION_PACKAGES = StringUtils
                .isBlank(VAL_CLASS_ANNOTATION_PACKAGE) ? null
                        : Arrays.asList(
                                VAL_CLASS_ANNOTATION_PACKAGE.split("\\|"));
        // JAVA MAPPER
        VAL_JAVA_MAPPER_PACKAGE = properties
                .getProperty(KEY_JAVA_MAPPER_PACKAGE);
        VAL_JAVA_MAPPER_PROJECT = properties
                .getProperty(KEY_JAVA_MAPPER_PROJECT);

    }

    private PropertiesUtil() {
    }
}
